package com.cn.map;

import org.junit.Test;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.*;

/**
 * 观看源码
 */
public class MyObjTest {

    @Test
    public void testString(){
        String str1="123123";
        String str2="aaa";
        System.out.println(str1.compareTo(str2));

        Integer i1=2;
        Integer i2=5;
        System.out.println(i1.compareTo(i2));
    }

    @Test
    public void test01(){
        System.out.println((16&3288451));
        System.out.println(10>>1);
    }

    @Test
    public void hashcodeTest(){

    }

    @Test
    public void hashMapTest(){
        HashMap<String,String> map = new HashMap<String,String>();//数据结构：数组+链表+红黑树 组成，默认数组长度是16，
        for(int i=0;i<14;i++){
            map.put("key"+i,"values"+i); //计算hashcode算中插槽，有碰撞就变成链表保存在下一个，链表超过8时候转成红黑树
        }
        map.get("key1");
        map.clear();//断开数组中的引用，数组还是保留

        Hashtable<String,String>  hashtable =new Hashtable<>();
        hashtable.put("name","小明"); //put方法使用 synchronized 来修饰
        hashtable.get("name"); //get 方法使用 synchronized 来修饰
        hashtable.clear(); //几乎所有操作的方法都使用synchronized来修饰

        HashSet<String> set= new HashSet<>(); //hashset 里面使用了一个HashMap来保存数据，
        set.add("x1");  //点进源码看，add()方法把值存放到HashMap中的key中去，达到去重效果

    }

    @Test
    public void arrayListTest(){
        ArrayList<String> list = new ArrayList<>();
        for(int i=0;i<10;i++){
            list.add("val"+i);
        }
        list.get(0);
        list.clear();
    }

    @Test
    public void linkedListTest(){
        LinkedList<String> list = new LinkedList<>(); //双向列表LinkedList，里面有两个头尾对象first、last，
        for(int i=0;i<10;i++){
            list.add("val-"+i); //新增数据时,如果头尾是空，头尾都指向它，否则就是
        }
        list.get(4);
        list.remove(6);
        for(String s:list){
            System.out.println(s);
        }
        System.out.println();
        Iterator it = list.iterator();
        while (it.hasNext()){
            Object obj = it.next();
            System.out.println(obj);
        }
//        list.clear();
    }

    @Test
    public void linkedHashMapTest(){
        LinkedHashMap<String,String> map =new LinkedHashMap<>();
        for(int i=0;i<15;i++){
            map.put("key"+i,"val"+i);
        }
        map.get("key1");
        map.clear();
    }

    @Test
    public void concurrentHashMapTest(){

        ConcurrentHashMap<String,String> map=new ConcurrentHashMap<>();
        for(int i=0;i<15;i++){
            map.put("key"+i,"val"+i);
        }
        map.get("key1");
        map.clear();
    }

    public void concurrentObj(){
//        float i=3.4F;
//        double d= 3.4;
        ThreadLocal threadLocal= new ThreadLocal();
        threadLocal.set(1);
        threadLocal.get();

        Thread t = Thread.currentThread();

        ArrayBlockingQueue arrayBlockingQueue =new ArrayBlockingQueue(10);

//        CompletableFuture // Future 可以运行复杂的下载情况
        CountDownLatch  count = new CountDownLatch(10);
        count.countDown();
//        BlockingDeque LinkedBlockingDeque
//        BlockingQueue LinkedBlockingQueue

//        ConcurrentHashMap
//        ConcurrentLinkedQueue
//        ConcurrentLinkedDeque
//        ConcurrentSkipListMap
//        ConcurrentSkipListSet
//        CopyOnWriteArrayList
//        CopyOnWriteArraySet

//        Executors
//        ForkJoinPool
//        ForkJoinTask
    }

    @Test
    public void linkedBlockingQueueTest() throws InterruptedException {
        LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>();
        for(int i=0;i<10;i++){
            queue.put("小明-"+i);
        }
        new Thread(() -> {
            String str;
            boolean sign=true;
            while (sign){
                try {
                    str =  queue.take();
                    System.out.println(str);
                    if(str.equals("小明-9")){
                        sign=false;
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Test
    public void ArrayBlockingQueueTest() throws InterruptedException {
        ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(10);
        for(int i=0;i<10;i++){
            queue.put("小明2-"+i);
        }
        new Thread(() -> {
            String str;
            boolean sign=true;
            while (sign){
                try {
                    str = queue.take();
                    System.out.println(str);
                    if(str.equals("小明2-9")){
                        sign=false;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    @Test
    public void test02(){
        for(int i=0;i<30;i++){
            System.out.println((8-1)&i);
        }
    }
}
